package com.gagakuai.bpm.config.mybatisplus.handler;

import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import com.gagakuai.bpm.utils.DateUtils;
import org.apache.ibatis.reflection.MetaObject;
import org.springframework.stereotype.Component;

import java.util.Date;

/*
 * mybatis plus 插入或更新对象时，自动添充一些值
 *
 * bpm
 */
@Component
public class StriveMetaObjectHandler implements MetaObjectHandler {

    private static final String CREATE_TIME = "createTime";
    private static final String UPDATE_TIME = "updateTime";

    /*
     * 插入时也要插入更新字段
     */
    @Override
    public void insertFill(MetaObject metaObject) {
        this.fillStrategy(metaObject,CREATE_TIME, DateUtils.formatDate(new Date()));
        this.fillStrategy(metaObject, UPDATE_TIME, DateUtils.formatDate(new Date()));
    }

    /*
     * 更新时只更新更新字段
     */
    @Override
    public void updateFill(MetaObject metaObject) {
        this.fillStrategy(metaObject, UPDATE_TIME, new Date());
        // to do
    }
}
